package com.wy.component.wxUtils;


/**
 * @author zhusw
 * @description TODO(通讯录标签管理类)
 * 2016年2月26日 下午10:46:18
 */
public class MTag {
	//创建标签地址
	public static String CREATE_TAG_URL = "https://qyapi.weixin.qq.com/cgi-bin/tag/create?access_token=ACCESS_TOKEN";
	//更新标签地址
	public static String UPDATA_TAG_URL = "https://qyapi.weixin.qq.com/cgi-bin/tag/update?access_token=ACCESS_TOKEN";
	//删除标签地址
	public static String DELETE_TAG_URL = "https://qyapi.weixin.qq.com/cgi-bin/tag/delete?access_token=ACCESS_TOKEN&tagid=ID";
	//获取标签成员地址
	public static String GET_TAG_PERSON = "https://qyapi.weixin.qq.com/cgi-bin/tag/get?access_token=ACCESS_TOKEN&tagid=ID";
	//增加标签成员地址
	public static String ADD_TAG_PERSON = "https://qyapi.weixin.qq.com/cgi-bin/tag/addtagusers?access_token=ACCESS_TOKEN";
	//删除标签成员地址
	public static String DELETE_TAG_PERSON = "https://qyapi.weixin.qq.com/cgi-bin/tag/deltagusers?access_token=ACCESS_TOKEN";
	
	
	/**
	 * 创建标签
	 * @param tagname 标签名称。长度为1~64个字符，标签不可与其他同组的标签重名，也不可与全局标签重名
	 * @param id 
	 * */
	public static String create_tag(String tagname, String tagid){
		String postJson = "{\"tagname\":\""+tagname+"\",\"tagid\":\""+tagid+"\"}";
		return String.format(postJson,tagname);
	}
	
	/**
	 * 更新标签名字
	 * @param tagid 标签ID
	 * @param tagname 标签名称。最长64个字符
	 * */
	public static String update_tag(String tagid , String tagname){
		String postJson = "{\"tagid\":\""+tagid+"\",\"tagname\":\""+tagname+"\"}";
		return String.format(postJson,tagid,tagname);
	}
	
	/**
	 * 删除标签
	 * @param tagid 标签ID
	 * */
	public static String delete_tag(String tagid){
		String delete_url = DELETE_TAG_URL.replace("ID", tagid);
		return delete_url;
	}
	
	/**
	 * 获取标签成员
	 * @param tagid 标签ID
	 * */
	public static String get_tag_person(String tagid){
		String get_tagperson_url = GET_TAG_PERSON.replace("ID", tagid);
		return get_tagperson_url;
	}
	
	/**
	 * 增加标签成员
	 * @param tagid 标签ID
	 * @param userlist 企业员工ID列表 格式："userlist":[ "user1","user2"]
	 * */
	public static String add_tag_person(String tagid,String userlist){
		String postJson = "{\"tagid\":\""+tagid+"\",\"userlist\":"+userlist+"}";
		return String.format(postJson, tagid,userlist);
	}
	
	/**
	 * 删除标签成员
	 * @param tagid 标签ID
	 * @param userlist 企业员工ID列表 格式："userlist":[ "user1","user2"]
	 * */
	public static String delete_tag_person(String tagid,String userlist){
		String postJson = "{\"tagid\":\""+tagid+"\",\"userlist\":"+userlist+"}";
		return String.format(postJson, tagid,userlist);
	}
	//示例
	public static void main(String[] args) {
		/**
		 * 创建标签示例
		 * */
		// 调取凭证
		String access_token = WeixinUtil.getAccessToken(new ParamesAPI().getCorpId(), new ParamesAPI().getSecret()).getToken();
		
		String postData = create_tag("测试标签1","100");
		int result = WeixinUtil.PostMessage(access_token, "POST", CREATE_TAG_URL, postData);//创建标签
		
//		String postData = update_tag("7","测试标签2");
//		int result = WeixinUtil.PostMessage(access_token, "POST", UPDATA_TAG_URL, postData);//更新标签
		
//		String postData = delete_tag("7");
//		int result = WeixinUtil.PostMessage(access_token, "POST", DELETE_TAG_URL, postData);//删除标签
		
//		String postData = get_tag_person("1");
//		int result = WeixinUtil.PostMessage(access_token, "POST", GET_TAG_PERSON, postData);//根据标签ID获取标签成员
		
//		String postData = add_tag_person("7","[\"10022\"]");
//		int result = WeixinUtil.PostMessage(access_token, "POST", ADD_TAG_PERSON, postData);//根据标签ID新增标签成员
		
		
		//每个管理组有个secret，即添加标签所属管理组
//		String postData = delete_tag_person("7","[\"10022\"]");
//		int result = WeixinUtil.PostMessage(access_token, "POST", DELETE_TAG_PERSON, postData);//根据标签ID删除标签成员
		
		
		// 打印结果
		if(0==result){
			System.out.println("操作成功");
		}
		else {
			System.out.println("操作失败");
		}
	}

}
